<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="clearSearch" />
      <el-button type="primary" @click="check">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="openDialog">添加员工</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="180" prop="name" />
        <el-table-column label="登录账号" width="180" prop="userName" />
        <el-table-column label="联系方式" width="120" prop="phonenumber" />
        <el-table-column label="角色" width="120" prop="roleName" />
        <el-table-column label="状态">
          <template #default="scope">
            {{ scope.row.status }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editEmployee(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delEmployee(scope.row.id)">删除</el-button>
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      layout="total, prev, pager, next"
      :page-size="params.pageSize"
      :total="total"
      @current-change="pageChange"
    />
    <addemployee
      ref="addTool"
      :dialog-show="dialogShow"
      @closeDialog="closeDialog"
    />

  </div>
</template>

<script>
import { getEmployeeListAPI, delEmployeeAPI } from '@/api/employee'
import addemployee from '@/views/System/Employee/addemployee.vue'

export default {
  name: 'Employee',
  components: {
    addemployee
  },
  data() {
    return {
      total: 0,
      employeeList: [],
      params: {
        page: 1,
        pageSize: 5,
        name: '' // 员工姓名
      },
      dialogShow: false
    }
  },
  mounted() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      const res = await getEmployeeListAPI(this.params)
      this.employeeList = res.data.rows
      this.total = res.data.total
    },
    clearSearch() {
      this.params.page = 1
      this.getEmployeeList()
    },
    pageChange(page) {
      this.params.page = page
      this.getEmployeeList()
    },
    closeDialog() {
      this.dialogShow = false
      this.getEmployeeList()
    },
    openDialog() {
      this.dialogShow = true
    },
    delEmployee(id) {
      this.$confirm('确定删除吗？', '警告').then(async() => {
        await delEmployeeAPI(id)
        this.getEmployeeList()
        this.$message.success('用户删除成功')
      }).catch()
    },
    editEmployee(id) {
      this.dialogShow = true
      this.$refs.addTool.getEmployeeDetail(id)
    },
    check() {
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
